package com.tianpengtech.common.util;

import com.jfinal.upload.UploadFile;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.common.upload.QiNiuUpload;

import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.util.Date;
import java.util.UUID;


public class ImageUtils {

    private static final String extList =".jpg.jpeg.png.bmp.gif";

    /*
        获取文件的后缀名称
     */
    private static String getFileExt(String v){

        String ext="";
        if(StringUtils.isNotEmpty(v)){
            int extPosition = v.lastIndexOf(".");
            ext = v.substring(extPosition);
        }
        return ext;
    }


    /*
        检查给定的后缀是否为合法的后缀名
     */
    public static boolean isAvailableExt(String ext) throws ValidateException {

        if(!extList.contains(ext)){
            throw new ValidateException("当前只接受.jpg,.jpeg,.png,.bmp,.gif格式的后缀");
        }
        return true;
    }


    public static boolean isAvailableSize(long size) throws ValidateException {
        if(size>1024*1024*20){
            throw new ValidateException("当前只接受20M的方法");
        }
        return true;
    }

    /*
        生成上传文件名称
     */
    private static String buildFileName(){
        UUID uuid = UUID.randomUUID();
        return uuid.toString();
    }

    private static String buildDistPath(String dirName,String ext){

        return File.separator+dirName+File.separator+buildFileName()+ext;
    }

    /*
        修改上传文件的名称
     */
    public static void rename(String sourcePath,String distPath){

        if(StringUtils.isNotEmpty(sourcePath) && StringUtils.isNotEmpty(distPath)){
            File sourceFile = new File(sourcePath);
            File distFile = new File(distPath);
            if(!distFile.getParentFile().exists()){
                distFile.getParentFile().mkdirs();
            }
            sourceFile.renameTo(distFile);
        }
    }

    public static String upload(UploadFile file,String uploadPath,String category) throws ValidateException {
        String fileName="";
        if(file!=null){
            String uploadFile = file.getFileName();
            String ext = getFileExt(uploadFile);
                if(isAvailableExt(ext) && isAvailableSize(file.getFile().length())){
                    String uploadParentPath = file.getFile().getParent();
                    String distParentPath = category+File.separator+DateUtils.formateData(new Date(), DateUtils.YYMM_SLASH_FORMAT);
                    String distPath = buildDistPath(distParentPath,ext);
                    if(uploadPath!=null) {//判断当前文件是否存在，假如存在,就及时删除此文件
                       uploadPath = uploadPath.replace("/",File.separator);
                       File tempFile = new File(uploadParentPath+uploadPath);
                       if(tempFile.exists()){
                           tempFile.delete();
                       }
                    }
                    rename(file.getFile().getPath(),uploadParentPath+distPath);
                    fileName = distPath.replace(File.separator,"/");
                }
        }
        return fileName;
    }
    
    public static String uploadToQiNiu(UploadFile file,String uploadPath,String category) throws ValidateException {
    	String fileName=upload(file,uploadPath,category);
    	String uploadParentPath = file.getFile().getParent();
    	String path = uploadParentPath+fileName;
    	fileName = QiNiuUpload.upload(path);
    	File temp = new File(path);
    	if(temp!=null){
    		temp.delete();
    	}
        return fileName;
    }
}
